import Geo from './Geo.js';
import Mat from './Mat.js';


export default class GameObject {

    public parent: GameObject | null = null;
    public children: GameObject[] = [];
    public name: string;

    public constructor(name: string) {
        this.name = name;
    }

    public init(gl: any) {

    }

    public update(time: number) {
        const { children } = this;
        for (let obj of children) {
            obj.update(time);
        }
        // 如果还有子类则调用update
        // super.update(time);
    }

    public updateEnd(time: number) {
        const { children } = this;
        for (let obj of children) {
            obj.updateEnd(time);
        }
    }

    public addOfInit(gl: any, ...child: GameObject[]) {
        const { children } = this;
        for (let obj of child) {
            obj.parent = this;
            children.push(obj);
            obj.init(gl);
        }
    }

    public add(...child: GameObject[]) {
        const { children } = this;
        for (let obj of child) {
            obj.parent = this;
            children.push(obj);
        }
    }

    public remove(element: GameObject) {
        const { children } = this;
        const i = children.indexOf(element);

        if (i != -1) {
            children.splice(i, 1);
            return true;
        }
        return false;
    }

    public getChildByName(name: string): GameObject | null {
        const { children } = this;
        let cur: GameObject | null = null;
        children.forEach(u => {
            if (u.name == name) {
                cur = u;
            }
        });
        return cur;
    }

}
